/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.com.apresentacao.midiateca;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import src.com.dados.midiateca.MaterialDidaticoDAO;
import src.com.negocio.midiateca.MaterialDidatico;

/**
 *
 * @author Camila Santos
 */
public class WMaterialDidatico extends javax.swing.JFrame {

    /**
     * Creates new form WMaterialDidatico
     */
    public WMaterialDidatico() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLTipoAquisicao = new javax.swing.JLabel();
        jTCodigo = new javax.swing.JTextField();
        jTipoMaterial = new javax.swing.JTextField();
        jTTitulo = new javax.swing.JTextField();
        jTQuantidade = new javax.swing.JTextField();
        jTAutor = new javax.swing.JTextField();
        jTFornecedor = new javax.swing.JTextField();
        jTPreco = new javax.swing.JTextField();
        jTTipoAquisicao = new javax.swing.JTextField();
        jBNovo = new javax.swing.JButton();
        jBBuscar = new javax.swing.JButton();
        jBAtualizar = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Cadastro de Material"));

        jLabel1.setText("Código");

        jLabel2.setText("Tipo de Material");

        jLabel3.setText("Título");

        jLabel4.setText("Quantidade");

        jLabel5.setText("Autor");

        jLabel6.setText("Fornecedor");

        jLabel7.setText("Preço");

        jLTipoAquisicao.setText("Tipo de Aquisição");

        jTCodigo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTCodigoActionPerformed(evt);
            }
        });

        jBNovo.setText("Novo");
        jBNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBNovoActionPerformed(evt);
            }
        });

        jBBuscar.setText("Buscar");
        jBBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBBuscarActionPerformed(evt);
            }
        });

        jBAtualizar.setText("Atualizar");
        jBAtualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBAtualizarActionPerformed(evt);
            }
        });

        jButton1.setText("Excluir");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLTipoAquisicao)
                    .addComponent(jLabel7)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTipoMaterial)
                    .addComponent(jTQuantidade)
                    .addComponent(jTAutor)
                    .addComponent(jTFornecedor)
                    .addComponent(jTPreco)
                    .addComponent(jTTitulo)
                    .addComponent(jTTipoAquisicao, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jTCodigo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBBuscar)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(151, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBAtualizar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBNovo)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBBuscar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTipoMaterial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jTTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTQuantidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTAutor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jTFornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jTPreco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLTipoAquisicao)
                    .addComponent(jTTipoAquisicao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBNovo)
                    .addComponent(jBAtualizar)
                    .addComponent(jButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBNovoActionPerformed
        // TODO add your handling code here:
        MaterialDidatico novoMaterial = new MaterialDidatico();

        novoMaterial.setCodigo(Integer.valueOf(jTCodigo.getText()));
        novoMaterial.setTipoMaterial(jTipoMaterial.getText());
        novoMaterial.setTitulo(jTTitulo.getText());
        novoMaterial.setQuantidade(Integer.valueOf(jTQuantidade.getText()));
        novoMaterial.setAutor(jTAutor.getText());
        novoMaterial.setFornecedor(jTFornecedor.getText());
        novoMaterial.setPreco(Float.valueOf(jTPreco.getText()));
        novoMaterial.setTipoAquisicao(jTTipoAquisicao.getText());

        MaterialDidaticoDAO materialDidaticoDAO = new MaterialDidaticoDAO();

        materialDidaticoDAO.cadastraMaterial(novoMaterial);
        jTCodigo.setText("");
        jTipoMaterial.setText("");
        jTTitulo.setText("");
        jTQuantidade.setText("");
        jTAutor.setText("");
        jTFornecedor.setText("");
        jTPreco.setText("");
        jTTipoAquisicao.setText("");
    }//GEN-LAST:event_jBNovoActionPerformed

    private void jTCodigoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTCodigoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTCodigoActionPerformed

    private void jBAtualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBAtualizarActionPerformed
        MaterialDidatico novoMaterial = new MaterialDidatico();

        novoMaterial.setCodigo(Integer.valueOf(jTCodigo.getText()));
        novoMaterial.setTipoMaterial(jTipoMaterial.getText());
        novoMaterial.setTitulo(jTTitulo.getText());
        novoMaterial.setQuantidade(Integer.valueOf(jTQuantidade.getText()));
        novoMaterial.setAutor(jTAutor.getText());
        novoMaterial.setFornecedor(jTFornecedor.getText());
        novoMaterial.setPreco(Float.valueOf(jTPreco.getText()));
        novoMaterial.setTipoAquisicao(jTTipoAquisicao.getText());

        MaterialDidaticoDAO materialDidaticoDAO = new MaterialDidaticoDAO();

        if (materialDidaticoDAO.atualizarMaterial(novoMaterial)) {
            jTCodigo.setText("");
            jTipoMaterial.setText("");
            jTTitulo.setText("");
            jTQuantidade.setText("");
            jTAutor.setText("");
            jTFornecedor.setText("");
            jTPreco.setText("");
            jTTipoAquisicao.setText("");
        }
    }//GEN-LAST:event_jBAtualizarActionPerformed

    private void jBBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBBuscarActionPerformed
        // TODO add your handling code here:
        MaterialDidatico materialDidatico;
        MaterialDidaticoDAO materialDidaticoDAO = new MaterialDidaticoDAO();
        try {
            materialDidatico = materialDidaticoDAO.buscarMaterial(Integer.valueOf(jTCodigo.getText()));
            jTipoMaterial.setText(materialDidatico.getTipoMaterial());
            jTTitulo.setText(materialDidatico.getTitulo());
            jTQuantidade.setText(String.valueOf(materialDidatico.getQuantidade()));
            jTAutor.setText(materialDidatico.getAutor());
            jTFornecedor.setText(materialDidatico.getFornecedor());
            jTPreco.setText(String.valueOf(materialDidatico.getPreco()));
            jTTipoAquisicao.setText(materialDidatico.getTipoAquisicao());
        } catch (SQLException ex) {
            Logger.getLogger(WMaterialDidatico.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_jBBuscarActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        MaterialDidaticoDAO materialDidaticoDAO = new MaterialDidaticoDAO();
        try {
            int resposta = javax.swing.JOptionPane.showConfirmDialog(null, "Deseja realmente excluir?");

            if (resposta == JOptionPane.YES_OPTION) {
                if (materialDidaticoDAO.excluirMaterial(Integer.valueOf(jTCodigo.getText()))) {
                    jTCodigo.setText("");
                    jTipoMaterial.setText("");
                    jTTitulo.setText("");
                    jTQuantidade.setText("");
                    jTAutor.setText("");
                    jTFornecedor.setText("");
                    jTPreco.setText("");
                    jTTipoAquisicao.setText("");
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(WMaterialDidatico.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(WMaterialDidatico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(WMaterialDidatico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(WMaterialDidatico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(WMaterialDidatico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new WMaterialDidatico().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBAtualizar;
    private javax.swing.JButton jBBuscar;
    private javax.swing.JButton jBNovo;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLTipoAquisicao;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTAutor;
    private javax.swing.JTextField jTCodigo;
    private javax.swing.JTextField jTFornecedor;
    private javax.swing.JTextField jTPreco;
    private javax.swing.JTextField jTQuantidade;
    private javax.swing.JTextField jTTipoAquisicao;
    private javax.swing.JTextField jTTitulo;
    private javax.swing.JTextField jTipoMaterial;
    // End of variables declaration//GEN-END:variables
}
